package com.itheima.web.listener;

import com.itheima.autoconfig.huawei.HuaWeiUGCTemplate;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.mongo.MovementService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(consumerGroup = "consumer1", topic = "tanhua-movement-check")
public class MovementCheckListener implements RocketMQListener<String> {

    @Reference
    private MovementService movementService;

    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;


    //当接收到消息(动态id),检测
    @Override
    public void onMessage(String movementId) {
        //1. 查询动态信息
        Movement movement = movementService.findMovementById(movementId);

        if (movement != null && movement.getState() != 1) {
            //2. 检测文字
            boolean textContentCheck = huaWeiUGCTemplate.textContentCheck(movement.getTextContent());

            //3. 检测图像
            boolean imageContentCheck = huaWeiUGCTemplate.imageContentCheck(movement.getMedias().toArray(new String[]{}));

            //4. 修改动态的状态
            if (textContentCheck && imageContentCheck) {
                movement.setState(1);//发布
            } else {
                movement.setState(2);//驳回
            }

            System.out.println("动态的审核结果为:" + movement.getState());
            movementService.updateMovement(movement);
        }
    }
}